import game.GameFieldPanel;
import game.GuessingPanel;
import game.MainController;
import game.MainModel;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.io.IOException;
import java.io.InputStream;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;


/**
 * @author Fredrik
 *
 */
public class WordChase extends Applet implements Observer {


	/**
	 * 
	 */
	private static final long serialVersionUID = -2713828050695692835L;

	private GameFieldPanel frameWork;
	
	private GuessingPanel guessingPanel;

	private JPanel mainPanel = new JPanel();

	private JMenu menu = new JMenu("File");

	private JMenuItem menuExit = new JMenuItem("Exit");

	private JMenuItem menuOpen = new JMenuItem("Open");
	
	private JMenuItem menuSave = new JMenuItem("Save world");

	private JMenuBar menuBar = new JMenuBar();
	

	/**
	 * Constructor creating a new Game main frame
	 * @param mainController
	 * @param model
	 */
	public void init( ) {
		MainModel model = new MainModel();
		MainController mainController = new MainController(model);
		
		try {
			InputStream is = this.getClass().getResourceAsStream( "/output/chalmers" );
			mainController.loadWorld(is);
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		//this.setTitle("Map editor");
		
		model.addObserver(this);
		
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.menuExit.setActionCommand(MainController.MENU_ACTION_EXIT);
		this.menuExit.addActionListener(mainController);
		this.menuSave.setActionCommand(MainController.MENU_ACTION_SAVE);
		this.menuSave.addActionListener(mainController);
		this.menuOpen.setActionCommand(MainController.MENU_ACTION_OPEN_FILE);
		this.menuOpen.addActionListener(mainController);

		this.menu.add(this.menuOpen);
		this.menu.add(this.menuSave);
		this.menu.add(this.menuExit);
		this.menuBar.add(this.menu);

		//this.setJMenuBar(this.menuBar);

		this.mainPanel.setLayout(new BorderLayout());
		//this.setSize(800, 600);
		
		this.guessingPanel = new GuessingPanel(model,mainController);
		
		this.frameWork = new GameFieldPanel(model);
		
		
		
		
		model.setEditingMode(false);
		

		this.mainPanel.add(this.frameWork, BorderLayout.CENTER);
		this.mainPanel.add(this.guessingPanel, BorderLayout.EAST);
		//setResizable(false);
		this.add(this.mainPanel);
		//pack();
	}

	/** (non-Javadoc)
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	public void update(Observable o, Object arg) {
		paintWorld();
	}

	
	/**
	 * Paint the world
	 */
	public void paintWorld(){
		
		repaint();
	}

	

}
